package mercurius.fitness.client.rb.measurement;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.locale.Locale;
import mercurius.fitness.client.rb.measurement.forms.AddMeasurementsForm;
import mercurius.fitness.client.rb.measurement.forms.EditMeasurementsForm;
import mercurius.fitness.client.rb.measurement.forms.ViewMeasurementForm;
import net.pleso.framework.client.bl.IAuthDataSource;
import net.pleso.framework.client.bl.actions.IDeleteRowExecutor;
import net.pleso.framework.client.bl.forms.IAddForm;
import net.pleso.framework.client.bl.forms.IEditForm;
import net.pleso.framework.client.bl.forms.IViewForm;
import net.pleso.framework.client.bl.providers.IActionProvider;
import net.pleso.framework.client.bl.providers.IActionProviders;
import net.pleso.framework.client.bl.providers.IAddFormProvider;
import net.pleso.framework.client.bl.providers.IDeleteRowProvider;
import net.pleso.framework.client.bl.providers.IEditFormProvider;
import net.pleso.framework.client.bl.providers.IViewFormProvider;
import net.pleso.framework.client.bl.rb.IRB;
import net.pleso.framework.client.bl.rb.columns.IRBColumn;
import net.pleso.framework.client.bl.rb.columns.impl.RBColumn;
import net.pleso.framework.client.dal.IDataRow;
import net.pleso.framework.client.dal.SelectParams;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class MeasurementsRB implements IRB, IActionProviders {
	
	
	
	private class AddRowAction implements IAddFormProvider {

		public IAddForm getAddForm(IDataRow row) {
			return new AddMeasurementsForm();
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class ViewRowAction implements IViewFormProvider {

		public IViewForm getViewForm(IDataRow row) {
			
			if (row instanceof Measurements && row != null) {
				return new ViewMeasurementForm(((Measurements)row).getID());
			}
			else
				return new ViewMeasurementForm(null);
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class EditRowAction implements IEditFormProvider {

		public IEditForm getEditForm(IDataRow row) {
			if (row instanceof Measurements && row != null)
				return new EditMeasurementsForm(((Measurements)row).getID());
			else
				return new EditMeasurementsForm(null);
		}

		public String getActionCaption() {
			return null;
		}
		
	}

	private class DeleteRowAction implements IDeleteRowProvider, IDeleteRowExecutor {

		public IDeleteRowExecutor getDeleteRowExecutor() {
			return this;
		}

		public String getActionCaption() {
			return null;
		}

		public void deleteRow(IDataRow row, AsyncCallback callback) {
			dataSet.delete(((Measurements) row).getID(), callback); 			
		}
		
	}

	private static MeasurementsDataSet dataSet = new MeasurementsDataSet();
	
	public static MeasurementsDataSet getDataSet() {
		return dataSet;
	}
	
	private class RBDataSource implements IAuthDataSource {
		
		public void select(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.select(params, callback);
		}

		public void selectCount(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.selectCount(callback);
		}
	}

	public String getCaption() {
		return Locale.constants().measurement_rb_caption();
	}

	private static RBColumn[] rbColumns = new RBColumn[] {
		//	new RBColumn(Measurements.Columns.id, 30),
			new RBColumn(Measurements.Columns.date, 30),
			new RBColumn(Measurements.Columns.weight, 30),
			new RBColumn(Measurements.Columns.fat, 30)
			
	};

	public IRBColumn[] getColumns() {
		return rbColumns;
	}

	public IAuthDataSource getDataSource() {
		return new RBDataSource();
	}

	public IActionProvider[] getActionProviders() {
		return new IActionProvider[] {
			//	new AddRowAction(),
				new ViewRowAction(),
				new DeleteRowAction(),
				new EditRowAction()
		};
	}

}
